<?php

namespace App\Http\Controllers;


use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;


class UserController extends Controller
{
	
	public function auth(Request $request){
        $code = $request->input('code','');
        if (empty($code)){
            return response()->json(['code'=>-1,'msg'=>'code参数不全']);
        }
        $appid = "wx1eb4ad94fd63a62b";
        $secret = "b5dffea197902db655bdba9cfcd7e90d";
        $url = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$appid.'&secret='.$secret.'&js_code='.$code.'&grant_type=authorization_cod';
        $res = curl_get($url);
        return $res;
        $data = json_decode($res, true);
        if ($data['errcode']==0){
            return response()->json(['code'=>0,'data'=>$data]);
        }else{
            return response()->json(['code'=>0,'msg'=>$data['errmsg']]);
        }

    }

    public function login(Request $request){
        $mobile = $request->input('mobile','');
        $password = $request->input('password','');
        if (empty($mobile)){
            return response()->json(['code'=>-10001,'msg'=>'手机号不能为空']);
        }
        if (empty($password)){
            return response()->json(['code'=>-10001,'msg'=>'密码不能为空']);
        }

        $user = DB::table('users')->where('mobile',$mobile)->first();
        if (!empty($user)){
            $user =  objectToArray($user);
            if ($user['passwords']!=$password){
                return response()->json(['code'=>-10001,'msg'=>'密码错误']);
            }
            $data['id'] = $user['id'];
            $data['mobile'] = $user['mobile'];
        }else{
            $data = [
                'mobile'=>$mobile,
                'passwords'=>$password,
                'created_at'=>date('Y-m-d H:i:s',time()),
                'updated_at'=>date('Y-m-d H:i:s',time()),
            ];
            $res = DB::table('users')->insertGetId($data);
            unset($data['passwords']);
            unset($data['created_at']);
            unset($data['updated_at']);
            $data['id'] = $res;
        }

        return response()->json(['code'=>0,'data'=>$data]);
    }


    public function editUser(Request $request){
        $user_id = $request->input('user_id','');
        $image = $request->input('image','');
        if (empty($user_id)){
            return response()->json(['code'=>-10001,'msg'=>'用户ID不能为空']);
        }

        $user = DB::table('users')->where('id',$user_id)->first();
        if (empty($user)){
            return response()->json(['code'=>-10001,'msg'=>'用户不存在']);
        }
        $user =  objectToArray($user);
        $data = [
            'image'=>base64img($image),
            'updated_at'=>date('Y-m-d H:i:s',time()),
        ];
        $res = DB::table('users')->where("id",$user_id)->update($data);
        $data['id'] = $user_id;
        $data['mobile'] =$user['mobile'];
        $data['image'] = config('app.url').'/upload/'.$data['image'];

        return response()->json(['code'=>0,'data'=>$data]);
    }

    public function myTrip(Request $request){
        $user_id = $request->input('user_id','');
        $select_type = $request->input('select_type',1);

        $user = DB::table('users')->where('id',$user_id)->first();
        if (empty($user)){
            return response()->json(['code'=>-10001,'msg'=>'用户不存在']);
        }
        $user =  objectToArray($user);

        $citys = DB::table('trip')
            ->where('status',1)
            ->where('user_id',$user_id)
            ->where('select_type',$select_type)
            ->orderBy('id','desc')->paginate(8);
        $citys =  objectToArray($citys);

        $data = [
            'select_type'=>$select_type,
            "trip_list"=>$citys
        ];
        return response()->json(['code'=>0,'data'=>$data]);



    }



	
    public function saveUser(Request $request){
        $name = $request->input('name','');
        $image = $request->input('image','');
        $openid = $request->input('openid','');
        if (empty($name) || empty($image) || empty($openid)){
            if (empty($category_id)){
                return response()->json(['code'=>10003,'msg'=>'参数不全']);
            }
        }
        $user = DB::table('users')->where('openid',$openid)->first();
        $user =  objectToArray($user);
        if (empty($user)){
            $data = [
                'name'=>$name,
                'image'=>$image,
                'openid'=>$openid,
                'created_at'=>date('Y-m-d H:i:s',time()),
                'updated_at'=>date('Y-m-d H:i:s',time()),
            ];
            $res = DB::table('users')->insertGetId($data);
            $data['id'] = $res;
        }else{
            $data = [
                'name'=>$name,
                'image'=>$image,
                'updated_at'=>date('Y-m-d H:i:s',time()),
            ];
            $res = DB::table('users')->where('openid',$openid)->update($data);
            $data['id'] = $user['id'];
            $data['openid'] = $user['openid'];
        }

        return response()->json(['code'=>0,'data'=>$data]);

    }

    public function getUser(Request $request){
        $user_id = $request->input('user_id',0);
        $user = DB::table('users')->where('id',$user_id)->first();
        $user =  objectToArray($user);

        return response()->json(['code'=>0,'data'=>$user]);
    }

    


}